

import java.io.*;
import java.sql.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 * Servlet implementation class for Servlet: UpdateCategory
 *
 */
 public class UpdateCategory extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
		
	 	protected Connection con;
	    protected PreparedStatement ps;
	    protected Statement s;
	    protected ResultSet rs;
	    protected String uvar = null;
		protected DbConnection dbconnect; 

		PrintWriter out;
		 		
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		out = response.getWriter();

		
		out.print("<html><body>");
		out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />");
		out.print("<div id=\"outer\">"
				+" <div id=\"wrapper\">"
				+" <div id=\"nav\">"
				+" <div id=\"nav-left\">"
				+" <div id=\"nav-right\">");
		out.print("<ul>"
	            +"<li><a href=index.jsp>Home</a></li>"
				+"<li><a href=InventoryList>List Items</a></li>"
	            +"<li><a href=addItem.jsp>Add New Item</a></li>"
	            +"<li><a href=CategoryList>List Categories</a></li>"
	            +"<li><a href=addCategory.jsp>Add New Category</a></li>"	
            +" </ul>");
		out.print("</div>");
		out.print("</div>");
		out.print("<div class=\"clear\"></div>");
		out.print("</div>");
		out.print("<div id=\"head-2\"></div>"
				+"<div id=\"login\">"
				+"<div id=\"login-bot\">");
		
		if(request.getParameter("catcnt") != null)//check if url variable "itemcnt" exists
		{
			uvar = request.getParameter("catcnt").trim();
			
			doForm(request, response);
		}
		else
			out.print("Error: No passed variable in url.<br/><a href=CategoryList>Back</a>");
		
		out.print("</body></html>");
	}  	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if (request.getParameter("updatecat") != null)
			doUpdate(request,response);
		else
			response.sendRedirect("CategoryList");
	} 
	
	protected void doForm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		out = response.getWriter();
		
		String s1 = null;
		String s2 = null;
		String s3 = null;


		try
		{
			dbconnect = new DbConnection();
			Class.forName(dbconnect.getDriver());
			con = dbconnect.getConnection();
			
			s = con.createStatement();
			rs = s.executeQuery(dbconnect.getQuery());
			

			while(rs.next()) 
			{
				s1 = rs.getString(1).trim();//category table PK
				s2 = rs.getString(2).trim();
				s3 = rs.getString(3).trim();

				
				
				if (s1.equals(uvar))
				{
					out.print("<form ");
					out.print(" method=\"post\">");
					out.print("<div class=ibm-column ibm-first>");
					out.print("<div class=ibm-container>");
					out.print("<h2 class=ibm-second>Update Category: "+s1+"</h2>" );
					out.print("<div class=ibm-container-body>");
					out.print("<div class=fm-opt1>");
					out.print("Category Code :&nbsp");
					out.print("<input type=\"text\" value=\"");
					out.print(s1);
					out.print("\" name=\"categoryCode\"></div><div class=fm-opt1>");
					out.print("Category Name :&nbsp");
					out.print("<input type=\"text\" value=\"");
					out.print(s2);
					out.print("\" name=\"categoryName\"></div><div class=fm-opt1>");
					out.print("Sub-category Code :&nbsp");
					out.print("<input type=\"text\" value=\"");
					out.print(s3);
					out.print("\" name=\"subcategoryCode\"></div>");
					
					out.print("<br><br><input type=\"submit\" name=\"updatecat\" value=\"Update Category\">");
					out.print("<input type=\"reset\" value=\"Reset\"></form><br/><a href=CategoryList>Back</a>" +
							"</form></div></div></div></div></div><br></body></html>");
					
					break;
				}
				
				
				
			}
			System.out.println(uvar);
			if (!s1.equals(uvar))
			{
				out.print("Row does not exist or no variable passed in url.<br/><a href=CategoryList>Back</a>");
			}
			
			s.close();
			con.close();
			out.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	protected void doUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		try
		{
			dbconnect = new DbConnection();
			Class.forName(dbconnect.getDriver());
			con = dbconnect.getConnection();
			

			
			ps = con.prepareStatement("update category set category_code=?, category_name=?, subcategory_code=? where category_code=?");
			ps.setString(1, request.getParameter("categoryCode"));
			ps.setString(2, request.getParameter("categoryName"));
			ps.setString(3, request.getParameter("subcategoryCode"));
			ps.setString(4, uvar);

			ps.executeUpdate();
			System.out.println("Update Success!");
			
			ps.close();
			con.close();
			
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			response.sendRedirect("CategoryList");
		}
		
	}
}